/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.applicationManag.controller;

import com.koron.workflow.applicationManag.bean.WhiteListBean;
import com.koron.workflow.applicationManag.bean.WhiteListQueryBean;
import com.koron.workflow.applicationManag.service.WhiteListService;
import com.koron.workflow.common.annotation.CaseUser;
import com.koron.workflow.common.bean.PageBean;
import com.koron.workflow.common.bean.StaffBean;
import com.koron.workflow.common.util.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@Api(value = "whiteList", description = "白名单")
@RestController
@RequestMapping("/whiteList")
public class WhiteListController {
    private static final Logger logger = LoggerFactory.getLogger(WhiteListController.class);

    @Autowired
    private WhiteListService service;

    @ApiOperation("白名单分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, dataType = "int", paramType = "query"),
    })
    @GetMapping("queryForList.htm")
    public MessageBean queryForList(@ApiIgnore String appId, @ApiIgnore Integer page, @ApiIgnore Integer pageSize) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        if (page != null){
            page = page - 1;
            if (page < 0){
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("页码不能小于1");
                return msg;
            }
        }
        try {
            PageBean<WhiteListQueryBean> pageBean = ADOConnection.runTask(service, "queryForList", PageBean.class,
                    appId, page * pageSize, pageSize);
            msg.setData(pageBean);
        } catch (Exception e) {
            logger.error("分页查询白名单失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

    @ApiOperation("根据ID查询白名单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "白名单id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/queryForObject.htm")
    public MessageBean queryForObject(@ApiIgnore String id) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PageBean.class);
        try {
            WhiteListQueryBean appBean = ADOConnection.runTask(service, "queryForObject", WhiteListQueryBean.class, id);
            msg.setData(appBean);
        } catch (Exception e) {
            logger.error("根据ID查询白名单失败", e);
            msg = MessageBean.create(BusinessCode.EXCEPTION.getCode(), BusinessCode.EXCEPTION.getDescription(), PageBean.class);
        }
        return msg;
    }

    @ApiOperation("保存白名单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "servername", value = "服务器名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "serverMAC", value = "服务器MAC地址", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "serverIP", value = "ip地址", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping("/save.htm")
    public MessageBean save(@ApiIgnore @CaseUser StaffBean user, @ApiIgnore @RequestBody @Valid WhiteListBean bean) {
        return ADOConnection.runTask(service, "save", MessageBean.class, user, bean);
    }

    @ApiOperation("修改白名单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "白名单id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "appId", value = "应用ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "servername", value = "服务器名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "serverMAC", value = "服务器MAC地址", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "serverIP", value = "ip地址", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping("/update.htm")
    public MessageBean update(@ApiIgnore @CaseUser StaffBean user, @ApiIgnore @RequestBody @Valid WhiteListBean bean) {
        return ADOConnection.runTask(service, "update", MessageBean.class, user, bean);
    }

    @ApiOperation("根据ID删除白名单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "白名单id", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping(value = "/delete.htm")
    public MessageBean delete(@ApiIgnore String id) {
        return ADOConnection.runTask(service, "delete", MessageBean.class, id);
    }
}
